package com.example.mytestapplication

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ProgressBar

class MainActivity : AppCompatActivity() {
    private val tagName = this.javaClass.simpleName
//    private val toastTools = ToastTools(this)     //直接获取类对象传递
    private val toastTools by lazy {                //延迟获取类对象传递
        ToastTools(this)
    }
    private val mBluetoothController = BluetoothController()

    private val mDeviceList = ArrayList<BluetoothDevice>()
    private val mBondedDeviceList = ArrayList<BluetoothDevice>()

    private val mProgressBar by lazy {
        findViewById<ProgressBar>(R.id.bluetoothFindDeviceProgressBar)
    }

    private val receiver = object:BroadcastReceiver() {     //广播接收器
        override fun onReceive(context: Context?, intent: Intent?) {

            //蓝牙状态改变检测代码
//            val state = intent?.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1)
//            toastTools.showToast(when (state) {
//                BluetoothAdapter.STATE_OFF -> "STATE_OFF"
//                BluetoothAdapter.STATE_ON -> "STATE_ON"
//                BluetoothAdapter.STATE_TURNING_ON -> "STATE_TURNING_ON"
//                BluetoothAdapter.STATE_TURNING_OFF -> "STATE_TURNING_OFF"
//                else -> "Unknown state"
//            })

            when (intent?.action) {
                BluetoothAdapter.ACTION_DISCOVERY_STARTED -> {
                    mProgressBar.visibility = View.VISIBLE
                    toastTools.showToast("ACTION_DISCOVERY_STARTED")
                }

                BluetoothAdapter.ACTION_DISCOVERY_FINISHED -> {
                    mProgressBar.visibility = View.INVISIBLE
                    toastTools.showToast("ACTION_DISCOVERY_FINISHED")
                }

//                BluetoothAdapter.AC
                //TODO
                //完成蓝牙查找到设备后添加设备到mDeviceList列表的逻辑代码

                else -> toastTools.showToast("action ?")
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)
        Log.d(tagName, "onCreate")

        //开机检查本机是否支持蓝牙
//        val mBluetoothAdapter:BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
//        if (mBluetoothAdapter == null) {
//            Log.d(tagName, "device does not support Bluetooth")
//            toastTools.showToast("device does not support Bluetooth")
//        } else {
//            Log.d(tagName, "device support Bluetooth")
//            toastTools.showToast("device support Bluetooth")
//        }

        val filter = IntentFilter()  //广播过滤器
//        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED)     //过滤蓝牙状态变化广播
//        filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED) //过滤??状态变化广播
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED)
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)
//        filter.addAction(BluetoothAdapter.ACTION)\
        //TODO
        //解决蓝牙查找到设备的广播过滤参数
        registerReceiver(receiver, filter)      //注册广播


    }

    fun isSupportBluetooth(view: View) {
        if (mBluetoothController.isSupportBluetooth())
            toastTools.showToast("support blue tooth")
        else
            toastTools.showToast("does not support blue tooth")
    }

    fun isBluetoothOn(view: View) {
        if (mBluetoothController.getBluetoothStatus())
            toastTools.showToast("bluetooth is on")
        else
            toastTools.showToast("bluetooth is off")
    }

    fun turnOnBluetooth(view: View) {
        mBluetoothController.turnOnBluetooth(this, 0)
    }

    fun turnOffBluetooth(view: View) {
        mBluetoothController.turnOffBluetooth(this, 0)
        toastTools.showToast("bluetooth off")
    }

    fun startDiscoveryBluetooth(view: View) {
        mBluetoothController.findDevice()
        toastTools.showToast("discovery bluetooth device")
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK)
            toastTools.showToast("蓝牙打开成功")
        else
            toastTools.showToast("蓝牙打开失败")
    }
}